<?php

namespace App\Repositories;

use App\Models\Record;
use App\Services\Command;
use Illuminate\Support\Facades\Auth;

/**
 * 上线记录
 */
class RecordRepository
{
    /**
     * 保存记录
     *
     * @param Command $commandObj
     * @param $task_id
     * @param $action
     * @param $duration
     * @return mixed
     */
    public static function saveRecord(Command $commandObj, $task_id, $action, $duration)
    {
        $record = new Record();
        return $record->fill([
            'user_id' => Auth::id(),
            'task_id' => $task_id,
            'status' => (int)$commandObj->getExeStatus(),
            'action' => $action,
            'created_at' => time(),
            'command' => var_export($commandObj->getExeCommand(), true),
            'memo' => substr(var_export($commandObj->getExeLog(), true), 0, 65530),
            'duration' => $duration,
        ])->save();
    }

    /**
     * 删除所有指定任务记录
     * @param $taskId
     * @return mixed
     */
    public static function deleteAllTask($taskId)
    {
        return Record::where("task_id", $taskId)->delete();
    }
}